package com.zzy.mccjt.service.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzy.mccjt.entity.system.SystemUser;
import com.zzy.mccjt.pojo.common.CurrentUser;
import com.zzy.mccjt.service.common.framework.response.WebApiResponse;
import com.zzy.mccjt.service.common.util.UserUtils;
import com.zzy.mccjt.service.dao.UserMapper;
import com.zzy.mccjt.service.dao.UserRoleMapper;
import com.zzy.mccjt.service.service.ResourcesService;
import com.zzy.mccjt.service.service.RoleService;
import com.zzy.mccjt.service.service.SystemMenuService;
import com.zzy.mccjt.service.service.UserService;
import com.zzy.pojo.system.login.UserDTO;
import com.zzy.pojo.system.login.UserPageDTO;
import com.zzy.pojo.system.login.resp.SystemMenuRespDTO;
import com.zzy.pojo.system.login.resp.SystemRoleRespDTO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleService roleService;

    @Autowired
    private SystemMenuService systemMenuService;

    @Autowired
    private ResourcesService resourcesService;



    @Override
    public WebApiResponse<UserDTO> getUserInfo() {
        UserDTO userDTO = new UserDTO();
        CurrentUser currentUser = UserUtils.currentUser();
        SystemUser user = new SystemUser();
        user.setId(currentUser.getUserId());
        user = userMapper.selectOne(user);
        if (null == user){
            return WebApiResponse.error("用户不存在,获取用户信息失败");
        }
        BeanUtils.copyProperties(user,userDTO);
        List<SystemRoleRespDTO> systemRoleRespDTOS = roleService.selectSystemRoleInfoByUserId(user.getId());
        userDTO.setRoles(systemRoleRespDTOS);
        if (CollectionUtils.isNotEmpty(systemRoleRespDTOS)){
            List<Long> roleIds = systemRoleRespDTOS.stream().map(m -> m.getId()).collect(Collectors.toList());
            List<SystemMenuRespDTO> systemMenuRespDTOList = systemMenuService.selectSystemMenufoByRoleId(roleIds);
            if (CollectionUtils.isNotEmpty(systemMenuRespDTOList)){
                userDTO.setMenus(systemMenuRespDTOList);
            }else {
                return WebApiResponse.error("用户没有菜单权限，获取用户信息失败，请配置用户菜单权限");
            }
        }else {
            return WebApiResponse.error("用户角色不存在，获取用户信息失败，请配置用户角色信息");
        }
        return WebApiResponse.success(userDTO);
    }

    @Override
    public WebApiResponse<PageInfo<UserDTO>> getPageList(UserPageDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<UserDTO> list = userMapper.getPageList(dto);
        PageInfo<UserDTO> page = new PageInfo<>(list);
        return WebApiResponse.success(page);
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return null;
    }

}
